package com.goods.controller.business;


import com.github.pagehelper.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.service.CategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.business.ProductCategoryVO;
import com.goods.common.vo.system.DepartmentVO;
import com.goods.common.vo.system.MenuNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 物资类别
 */
@Api(tags = "业务管理-物资类别接口")
@RestController
@RequestMapping("/business/productCategory")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //加载分类数据的分类树
    @ApiOperation(value = "加载分类数据")
    @GetMapping("/categoryTree")
    public ResponseBean getCategoryList(
             ){
        //加载分类数据
        List<ProductCategoryTreeNodeVO> list = categoryService.getCategoryListTree();
        PageVO<ProductCategoryTreeNodeVO> pageVo = new PageVO<>(list.size(), list);
        return ResponseBean.success(pageVo);
}

    //加载父级分类数据
    @ApiOperation(value = "加载父类分类数据")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean getParentCategoryTree(){
        List<ProductCategoryTreeNodeVO> list = categoryService.getParentCategoryTree();
        return ResponseBean.success(list);
    }

    //添加分类
    @ApiOperation(value = "添加分类")
    @PostMapping("/add")
    public ResponseBean add(@RequestBody ProductCategoryVO productCategoryVO){
        try {
            categoryService.add(productCategoryVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error();
        }
        return ResponseBean.success();
    }

    //打开修改页面弹出框,并且回显数据
    @ApiOperation(value = "修改信息的回显")
    @GetMapping("/getEdit/{id}")
    public ResponseBean getEdit(@PathVariable Long id){
        ProductCategoryVO productCategoryVO = categoryService.getEdit(id);
        return ResponseBean.success(productCategoryVO);
    }

    //修改信息
    @ApiOperation(value = "修改信息")
    @PostMapping("/edit")
    public ResponseBean edit(@RequestBody ProductCategoryVO productCategoryVO){
       categoryService.edit(productCategoryVO);
       return ResponseBean.success();
    }

    //删除分类
    @ApiOperation(value = "删除分类")
    //@DeleteMapping("/delete/{id}")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResponseBean delete(@PathVariable Long id){
        categoryService.delete(id);
        return ResponseBean.success();
    }
}
